package com.qsmx.qingqiuticket.app.delegate.back;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.qsmx.core.delegates.AtmosDelegate;
import com.qsmx.core.util.StringUtil;
import com.qsmx.core.util.storage.AtmosPreference;
import com.qsmx.qingqiuticket.app.R;
import com.qsmx.qingqiuticket.app.databinding.DelegateBackSettingBinding;
import com.qsmx.qingqiuticket.app.delegate.UsbPrintHomeDelegate;
import com.qsmx.qingqiuticket.app.event.RefreshSettingEvent;
import com.qsmx.qingqiuticket.app.fusion.FusionPre;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class BackSettingDelegate extends AtmosDelegate {

    private DelegateBackSettingBinding mBinding;

    @Override
    public Object setLayout() {
        return R.layout.delegate_back_setting;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mBinding = DelegateBackSettingBinding.bind(rootView);

        if (AtmosPreference.getCustomStringPre(FusionPre.LOGIN_TYPE).equals("1") || StringUtil.isEmpty(AtmosPreference.getCustomStringPre(FusionPre.LOGIN_TYPE))) {
            mBinding.tvLoginTypeStatus.setText("强制登录");
        } else {
            mBinding.tvLoginTypeStatus.setText("非强制登录");
        }

        onClick();
    }

    private void onClick() {
        mBinding.tvMachineNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSupportDelegate().start(MachineSettingDelegate.create(false));
            }
        });

        mBinding.tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSupportDelegate().pop();
            }
        });

        mBinding.tvLoginType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSupportDelegate().start(new BackLoginTypeDelegate());
            }
        });

        mBinding.tvPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSupportDelegate().start(new BackPasswordSetDelegate());
            }
        });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSettingEvent(RefreshSettingEvent event) {
        if (event != null && event.getData() != null) {
            if (AtmosPreference.getCustomStringPre(FusionPre.LOGIN_TYPE).equals("1") || StringUtil.isEmpty(AtmosPreference.getCustomStringPre(FusionPre.LOGIN_TYPE))) {
                mBinding.tvLoginTypeStatus.setText("强制登录");
            } else {
                mBinding.tvLoginTypeStatus.setText("非强制登录");
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }
}
